#include "Temperature_logger.h"

extern volatile unsigned int KEY_PRESS_DURATION_COUNT;
extern volatile unsigned int KEY_PRESS_DURATION_COUNT ;
extern volatile unsigned char KEY_PRESSED  ;
extern volatile unsigned char PRESENT_KEY ;
extern unsigned long int Measure_Int_In_Seconds;
static unsigned long int Int_Counter=0;
extern char Logging_Trigger;
extern char Update_Measure_Int_Flag;
extern unsigned char Auto_Power_Off_Val;	
extern Switch_State Auto_Power_Off_Switch;

volatile unsigned char Auto_Power_Off_Counter=30;

void RTC_init(void)
{
//Wait for sometime to stabilise

	cli();
	cbi(TIMSK2,TOIE2); 
	ASSR=(1<<AS2);
	TCNT2=0;
	TCCR2A|=(1<<CS22) | (1<<CS20);	//Select Prescaler = 128. Hence, 32768/128=256 (1 overflow)
	while ((ASSR&0x01)|(ASSR&0x04));
	TIFR2=0xFF;
	sbi(TIMSK2,TOIE2);
	sei();
	gSecond=0;
	gMinute=0;
	gHour=0;
	gDay=1;
	gMonth=1;
	gYear=13;

}

SIGNAL(SIG_OVERFLOW2)
{
	
	gSecond++;
	Logging_Trigger_Process(); // Logging trigger counter decrement every second...
	
	if(gSecond==60)
	{	gSecond=0;
		gMinute++;
		//if(Auto_Power_Off_Counter>0) //for 
		//Auto_Power_Off_Counter--;    //testing
		
		if(gMinute>59)
		{
			gMinute=0;
			gHour++;
			
			if(gHour>23)
			{
				gHour=0;
				gDay++;
				if(Auto_Power_Off_Counter>0)	//
				Auto_Power_Off_Counter--;		//
				if((gDay>28) && (gMonth==2)) //FEB--Leap year still to be considered!
				{
					gDay=1;
					gMonth++;
					
				}
				else if (((gDay>30) && ((gMonth==4)||(gMonth==4)||(gMonth==6)||(gMonth==9)||(gMonth==11)))) //APL,JUN,SEP,NOV
				{
					gDay=1;
					gMonth++;
				}
				else if (gDay>31)
				{
					gDay=1;
					gMonth++;
				}
			}
			
			if (gMonth>12)
			{
				gMonth=1;
				gYear++;
			}
		}
		
	}
}

void Ena_Key_Press_Dur_Count(void)
{
//	cli();
	cbi(TIMSK0,TOIE0); 
	TCNT0=0;
	TCCR0A|=(1<<CS02) | (1<<CS00);	//Select Prescaler = 1024. 
//	TCCR0A|= (1<<CS00);	//No prescaler. 
	TIFR0=0xFF;
	sbi(TIMSK0,TOIE0);
//	sei();
}

SIGNAL(SIG_OVERFLOW0)
{
	if ((PRESENT_KEY==KEY_INVALID))
		{
		KEY_PRESS_DURATION_COUNT=0;
		}
		else if ((KEY_PRESSED==PRESENT_KEY))
			{
			KEY_PRESS_DURATION_COUNT++;
			}
			else
				{
				KEY_PRESS_DURATION_COUNT=KEY_PRESS_DURATION_COUNT;
				}
}

void Dis_Key_Press_Dur_Count(void)
{
//	cli();
	cbi(TIMSK0,TOIE0); 
	TCNT0=0;
	TIFR0=0xFF;
	KEY_PRESS_DURATION_COUNT=0;
}

void Adjust_Time(void)
{
	if(gSecond>=60)
	{	gSecond=0;
	}
	if(gMinute>59)
	{
	gMinute=0;
	}
	if(gHour>23)
	{
	gHour=0;
	}
	if((gDay>28) && (gMonth==2)) //FEB--Leap year still to be considered!
	{
		gDay=1;
	}
	else if (((gDay>30) && ((gMonth==4)||(gMonth==4)||(gMonth==6)||(gMonth==9)||(gMonth==11)))) //APL,JUN,SEP,NOV
	{
		gDay=1;
	}
	else if (gDay>31)
	{
		gDay=1;
	}
	else if (gDay==0)
		gDay=1;
	
	if(gMonth>12)
	{
	gMonth=1;
	}
	else if (gMonth==0)
	gMonth=1;
}

void Logging_Trigger_Process(void)
{
	
 if (((Int_Counter==0)&&(Auto_Power_Off_Counter>0))||((Int_Counter==0)&&(Auto_Power_Off_Switch==Off_Sw)))
	{ 
		// Initiate temperature logging 
		Logging_Trigger = 1;
		// Reload counter;
		Int_Counter=Measure_Int_In_Seconds;
		
	}
else if (Update_Measure_Int_Flag==1)
	{
		Int_Counter=Measure_Int_In_Seconds;
		Update_Measure_Int_Flag=0; //Unset the flag
	}
else
	{
		Int_Counter-=1;
		Logging_Trigger = 0;
		
	}

}

